home *** CD-ROM | disk | FTP | other *** search
- /*------------------------------------------------------------
- | FILE NAME: Bytes.c
- |
- | DOCUMENT: [1011.0]
- |
- | PURPOSE: To provide source code for byte-addressed memory
- | access procedures.
- |
- | DESCRIPTION:
- |
- | NOTE:
- |
- | HISTORY: 02.03.93 by Lee Malone from Bytes.txt.
- | 08.28.93 converted to new-style declarations
- ------------------------------------------------------------*/
-
- #include <Bytes.h>
-
- /* ---------------------- EQUATES ------------------------- */
-
- /* ------------------------ DATA -------------------------- */
- Quad CountOfBytesParsed; /* Result count */
-
- /* --------------------- PROCEDURES ----------------------- */
-
- /*------------------------------------------------------------
- | NAME: CompareBytes
- |
- | PURPOSE: To compare two ranges of bytes based on their
- | unsigned numeric ordering.
- |
- | DESCRIPTION: Comparison operation.
- | Returns: 0 if string AA = string BB.
- | positive number if AA > BB.
- | negative number if AA < BB.
- |
- | EXAMPLE: Result = CompareBytes(A,ACount,B,BCount);
- |
- |
- | NOTE: You must cast the count parameters of 'CompareBytes'
- | to be a 'Quad' to avoid errors when using 'Think C'.
- |
- | If both byte ranges match over the length of the
- | shorter range, then the longer range follows the
- | shorter in terms of order.
- |
- | ASSUMES:
- |
- | HISTORY: 09.19.89 by Lee Malone
- | 02.15.93 changed count to quad.
- | 11.10.93 simplified logic
- ------------------------------------------------------------*/
- Comparison
- CompareBytes(AddressOfByte A,
- Quad ACount,
- AddressOfByte B,
- Quad BCount)
- {
- Quad MinCount;
- Byte AByte;
- Byte BByte;
-
- MinCount = ACount;
- if( BCount < MinCount ) MinCount = BCount;
-
- /* compare for the length of the shortest range */
- while( MinCount-- )
- {
- AByte = *A++;
- BByte = *B++;
-
- if(AByte != BByte)
- {
- return(((Comparison) AByte) - ((Comparison) BByte));
- }
- }
-
- /* if unequal length ranges with first MinCount characters equal */
- if( ACount != BCount )
- {
- return( (Comparison) ACount - (Comparison) BCount );
- }
-
- return((Comparison) 0); /* else ranges are equal */
- }
-
- /*------------------------------------------------------------
- | NAME: CopyBytes
- |
- | PURPOSE: To copy a range of bytes from one place to another.
- |
- | DESCRIPTION: Correctly handles overlapping series of bytes.
- |
- | EXAMPLE: CopyBytes( From, To, HowMany );
- |
- | NOTE: You must cast literal count parameters to be a 'Quad'
- | to avoid errors when using 'Think C'.
- |
- | ASSUMES:
- |
- | HISTORY: 08.25.89 by Lee Malone
- | 08.31.89 added overlapping capability
- | 02.03.93 assembler version derived from Forthmacs
- | via AM.
- | 09.22.93 replaced stack relative argument
- | addresses with names
- | 11.01.93 assembler version replaced with general
- | 'C' version.
- | See 'FastBytes.c' for assembler version.
- ------------------------------------------------------------*/
- Nothing
- CopyBytes( AddressOfByte From,
- AddressOfByte To,
- Quad Count )
- {
- if( From >= To )
- {
- while( Count-- )
- {
- *To++ = *From++;
- }
- }
- else
- {
- To += Count;
- From += Count;
-
- while( Count-- )
- {
- *--To = *--From;
- }
- }
-
- }
-
- /*------------------------------------------------------------
- | NAME: ExchangeBytes
- |
- | PURPOSE: To exchange the values in two non-overlapping
- | ranges of bytes.
- |
- | DESCRIPTION:
- |
- | EXAMPLE: ExchangeBytes( SourceA, SourceB, Count );
- |
- | NOTE:
- |
- | ASSUMES:
- |
- | HISTORY: 12.01.90 Create by Lee Malone from AM.
- | 09.19.91 revised for Focus.
- | 02.03.93 revised for WM.
- ------------------------------------------------------------*/
- Nothing
- ExchangeBytes( AddressOfByte SourceA,
- AddressOfByte SourceB,
- Quad Count )
- {
- Byte AByte;
-
- while( Count-- )
- {
- AByte = *SourceA;
- *SourceA = *SourceB;
- *SourceB = AByte;
- SourceA++;
- SourceB++;
- }
- }
-
- /*------------------------------------------------------------
- | NAME: FillBytes
- |
- | PURPOSE: To fill a range of bytes with a byte value.
- |
- | DESCRIPTION:
- |
- | EXAMPLE: FillBytes( Destination, ByteCount, ByteValue );
- |
- | NOTE: 'Pair' used as argument instead of 'Byte' because
- | Think C can't pass 'Byte' arguments properly. See
- | 'DataSize.h' for more.
- |
- | ASSUMES:
- |
- | HISTORY: 08.25.89 by Lee Malone
- | 11.10.93 changed argument types to 'Pair'.
- ------------------------------------------------------------*/
- Nothing
- FillBytes( AddressOfByte Destination,
- Quad ByteCount,
- Pair ByteValue )
- {
- while( ByteCount-- )
- {
- *Destination++ = ByteValue;
- }
- }
-
- /*------------------------------------------------------------
- | NAME: IsMatchingBytes
- |
- | PURPOSE: To tell if two ranges of bytes match in value.
- |
- | DESCRIPTION: Returns 'True' if they match, else 'False'.
- |
- | EXAMPLE:
- |
- | Result = IsMatchingBytes( SomeBytes, OtherBytes, 10 );
- |
- | NOTE:
- |
- | ASSUMES:
- |
- | HISTORY: 10.21.89 by Lee Malone
- | 02.15.93 changed count to quad.
- ------------------------------------------------------------*/
- Truth
- IsMatchingBytes( AddressOfByte AA,
- AddressOfByte BB,
- Quad ACount )
- {
- Truth Result;
-
- Result = True;
-
- while( ACount-- )
- {
- if(*AA != *BB )
- {
- Result = False;
- break;
- }
- AA++;
- BB++;
- }
- return( Result );
- }
-
- /*------------------------------------------------------------
- | NAME: ReplaceBytes
- |
- | PURPOSE: To replace all occurances of a byte in a range of
- | bytes.
- |
- | DESCRIPTION:
- |
- | EXAMPLE:
- | ReplaceBytes( TokenBuffer, (Quad) 100,
- | (Pair) 'a', (Pair) 'A' );
- |
- | NOTE: 'Pair' used as parameters instead of 'Byte' because
- | Think C can't pass 'Byte' arguments properly. See
- | 'DataSize.h' for more.
- |
- | ASSUMES:
- |
- | HISTORY: 04.04.91 by Lee Malone
- | 11.10.93 changed argument types to 'Pair'.
- ------------------------------------------------------------*/
- Nothing
- ReplaceBytes( AddressOfByte BaseAddress,
- Quad Count,
- Pair FindByte,
- Pair ReplaceWithByte )
- {
- while(Count--)
- {
- if( *BaseAddress == FindByte )
- {
- *BaseAddress = ReplaceWithByte;
- }
- BaseAddress++;
- }
- }
-